package com.changil.nowonchangil.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import javax.management.Query;

import com.changil.nowonchangil.dto.BibleWord;
import com.changil.nowonchangil.dto.User;

public class UserManager extends BaseTableManager{
	private static UserManager instance = null;
	final static String TABLE_NAME = "User";
	static enum  COLUMNS {
		id,
		password,
		name,
		joinDateTime
	}
	
	public static UserManager getsInstance() {
		if (instance == null) {
			synchronized (UserManager.class) { 
				if (instance == null) { 
					instance = new UserManager();
				}
			}
		}
		return instance;
	}
	
	public User select(String id, String password) {
		String sql = "select * from "+ TABLE_NAME
				+" WHERE "+ COLUMNS.id.toString() +" = ?"
				+" AND "+ COLUMNS.password.toString() + " = ?";
		Connection conn = createConnection();
		PreparedStatement statement = null;
		ResultSet result = null;
		User user = new User(); 
		try {
			statement = conn.prepareStatement(sql);
			statement.setString( 1, id );
			statement.setString( 2, password );
			result = statement.executeQuery();
			System.out.println("쿼리는 "+sql);
			
			while (result.next()){
				user.id = result.getString(result.findColumn(COLUMNS.id.toString()));
				user.name = result.getString(result.findColumn(COLUMNS.name.toString()));
				user.joinDateTime = result.getString(result.findColumn(COLUMNS.joinDateTime.toString()));
				System.out.println("id "+user.id);
				System.out.println("password "+user.name);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if(null != result) {
				try {
					result.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if(null != statement) {
				try {
					statement.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(null != conn) {
				try {
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
		return user;
	}
}
